home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / iconv8_l.arc / PROGS.ARC / miu.icn < prev    next >
Encoding:
Text File  |  1990-03-08  |  1.6 KB  |  65 lines

  1. ############################################################################
  2. #
  3. #    Name:    miu.icn
  4. #
  5. #    Title:    Generate strings from the MIU system
  6. #
  7. #    Author:    Cary A. Coutant, modified by Ralph E. Griswold
  8. #
  9. #    Date:    December 27, 1989
  10. #
  11. ############################################################################
  12. #
  13. #     This program generates strings from the MIU string system.
  14. #
  15. #     The number of generations is determined by the command-line argument.
  16. #  The default is 7.
  17. #
  18. #  Reference:
  19. #
  20. #     Godel, Escher, and Bach: an Eternal Golden Braid, Douglas R.
  21. #  Hofstadter, Basic Books, 1979. pp. 33-36.
  22. #
  23. ############################################################################
  24.  
  25. procedure main(arg)
  26.    local count, gen, limit
  27.  
  28.    count := 0
  29.    limit := integer(arg[1]) | 7
  30.    gen := ["MI"]
  31.    every count := 1 to limit do {
  32.       show(count,gen)
  33.       gen := nextgen(gen)
  34.       }
  35. end
  36.  
  37. # show - show a generation of strings
  38.  
  39. procedure show(count,gen)
  40.    write("Generation #",count)
  41.    every write("   ",image(\!gen))
  42.    write()
  43. end
  44.  
  45. # nextgen - given a generation of strings, compute the next generation
  46.  
  47. procedure nextgen(gen)
  48.    local new, s
  49.    new := set()
  50.    every insert(new,apply(!gen))
  51.    return sort(new)
  52. end
  53.  
  54. # apply - produce all strings derivable from s in a single rule application
  55.  
  56. procedure apply(s)
  57.    local i
  58.    if s[-1] == "I" then suspend s || "U"
  59.    if s[1] == "M" then suspend s || s[2:0]
  60.    every i := find("III",s) do
  61.       suspend s[1:i] || "U" || s[i+3:0]
  62.    every i := find("UU",s) do
  63.       suspend s[1:i] || s[i+2:0]
  64. end
  65.